package com.example.administrator.liveapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.liveapp.ApplicationUtils;
import com.example.administrator.liveapp.MainActivity;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.Xutils3.MyCallBack;
import com.example.administrator.liveapp.Xutils3.XUtils;
import com.example.administrator.liveapp.bean.LoginBean;
import com.example.administrator.liveapp.bean.RegisteredYzPageBean;
import com.example.administrator.liveapp.utils.AppUrl;
import com.example.administrator.liveapp.utils.SharedPrefsUtil;
import com.google.gson.Gson;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.util.LogUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 手机号验证码登录activity
 */

public class LoginPage extends Activity implements View.OnClickListener{

    private LinearLayout login_button;
    private EditText login_username;//输入手机号
    private EditText LoginCode_validation;//输入手机验证码
    private SharedPrefsUtil sp;
    private String username;//手机号
    private String userCode;//验证码
    private Button button_code;//获取手机验证码
    private LoginBean bean;
    private RegisteredYzPageBean CodeBean;
    private int value;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_code_page);
        ApplicationUtils applicationUtils = new ApplicationUtils();
        applicationUtils.addActivity(this);
        initView();
        listener();
    }

    private void initView() {
        login_button = (LinearLayout) findViewById(R.id.login_button);
        login_username = (EditText) findViewById(R.id.login_username);
        LoginCode_validation = (EditText) findViewById(R.id.LoginCode_validation);
        button_code = (Button) findViewById(R.id.button_code);
        findViewById(R.id.weixin).setOnClickListener(this);
    }

    /**
     * 倒计时:
     */
    private CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            button_code.setEnabled(false);
            button_code.setBackgroundColor(ContextCompat.getColor(LoginPage.this,R.color.code));
            button_code.setText((millisUntilFinished / 1000) + "秒后可重发");
        }
        @Override
        public void onFinish() {
            button_code.setEnabled(true);
            button_code.setBackgroundColor(ContextCompat.getColor(LoginPage.this,R.color.code2));
            button_code.setText("获取验证码");
        }
    };

    private void listener() {
        button_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.start();
                username = login_username.getText().toString();
                String codeUrl = AppUrl.URLS + AppUrl.RegistrationCodeURL;
                Map<String,String> map=new HashMap<>();
                map.put("User_Phone", username);
                map.put("Type", "5");
                XUtils.Get(codeUrl,map,new MyCallBack<String>(){
                    @Override
                    public void onSuccess(String result) {
                        super.onSuccess(result);
                        JSONObject jsonObject = null;
                        try {
                            jsonObject = new JSONObject(result);
                            String code = jsonObject.optString("code");
                            if(code.equals("4400")){
                                Gson gson = new Gson();
                                CodeBean = gson.fromJson(result, RegisteredYzPageBean.class);
                                value = CodeBean.getData().getValue();
                                Toast.makeText(LoginPage.this,"已发送验证码",Toast.LENGTH_SHORT).show();
                            }else{
                                Toast.makeText(LoginPage.this,"发送验证码失败",Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        super.onError(ex, isOnCallback);
                        LogUtil.e(ex.getMessage()+"联网请求失败");
                    }
                });
            }
        });

        login_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sp = new SharedPrefsUtil();
                username = login_username.getText().toString();
                userCode = LoginCode_validation.getText().toString();
                String url = AppUrl.URLS+AppUrl.LoginCodeURL;
                Map<String,String> map=new HashMap<>();
                map.put("consumerTel", username);
                map.put("identifying", userCode);
                XUtils.Get(url,map,new MyCallBack<String>(){
                    @Override
                    public void onSuccess(String result) {
                        super.onSuccess(result);
                        if(userCode.equals(""+value)){
                            Gson gson = new Gson();
                            Log.e("result","====="+result);
                            bean = gson.fromJson(result, LoginBean.class);
                            sp.putStringValue(LoginPage.this,"username",username);
                            Log.e("pass",bean.getPass()+"====="+result);
                            sp.putStringValue(LoginPage.this,"userpassword",bean.getPass());
                            sp.putStringValue(LoginPage.this,"consumerId",bean.getConsumerid());
                            sp.putStringValue(LoginPage.this,"consumerName",bean.getConsumername());
                            ApplicationUtils.getInstance().loginBean = bean;
                            Intent intent = new Intent();
                            intent.putExtra("phone",login_username.getText().toString());
                            intent.putExtra("passwords",userCode);
                            intent.setClass(LoginPage.this,MainActivity.class);
                            startActivity(intent);
                            overridePendingTransition(R.anim.zoomin,R.anim.zoomout);
                            Toast.makeText(getApplicationContext(),"登录成功",Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(getApplicationContext(),"用户名或验证码错误",Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        super.onError(ex, isOnCallback);
                        Toast.makeText(LoginPage.this, "请求失败", Toast.LENGTH_SHORT).show();
                    }
                });

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.weixin:
//                UMShareAPI.get(this).getPlatformInfo(this, SHARE_MEDIA.WEIXIN, weiXinUmAuthListener);
                startActivity(new Intent(this,WxLoginPage.class));
                overridePendingTransition(R.anim.zoomin,R.anim.zoomout);
                break;
        }
    }

    /**
     * 微信授权
     */
//    private UMAuthListener weiXinUmAuthListener = new UMAuthListener() {
//        @Override
//        public void onStart(SHARE_MEDIA share_media) {
//            Toast.makeText(LoginPage.this, "微信正在授权，请稍后...", Toast.LENGTH_SHORT).show();
//        }
//        @Override
//        public void onComplete(SHARE_MEDIA share_media, int i,
//                               final Map<String, String> map) {
//            Log.e("666666", map + "---" + map.get("openid"));
//            map.put("tid", "1");
//            String url = AppUrl.URL + AppUrl.LoginURL;
//            XUtils.Get(url, map, new MyCallBack<String>() {
//                @Override
//                public void onSuccess(String result) {
//                    super.onSuccess(result);
//                    JSONObject jsonObject = null;
//                    try {
//                        jsonObject = new JSONObject(result);
//                    } catch (JSONException e) {
//                        e.printStackTrace();
//                    }
//                    String message = jsonObject.optString("message");
//                    String results = jsonObject.optString("result");
//                    if (results.equals("true")) {
//                        Gson gson = new Gson();
//                        bean = gson.fromJson(result, LoginBean.class);
//                        sp.putStringValue(LoginPage.this, "openid",map.get("openid"));
//                        sp.putStringValue(LoginPage.this, "unionid",map.get("unionid"));
//                        sp.putStringValue(LoginPage.this, "screen_name",map.get("screen_name"));
//                        sp.putStringValue(LoginPage.this, "iconurl",map.get("iconurl"));
//                        ApplicationUtils.getInstance().loginBean = bean;
//                        Intent intent = new Intent();
//                        intent.setClass(LoginPage.this, MainActivity.class);
//                        startActivity(intent);
//                        overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
//                        Toast.makeText(LoginPage.this, "授权成功", Toast.LENGTH_SHORT).show();
//                    } else {
//                        Toast.makeText(LoginPage.this, message + "登录失败", Toast.LENGTH_SHORT).show();
//                    }
//                }
//
//                @Override
//                public void onError(Throwable ex, boolean isOnCallback) {
//                    super.onError(ex, isOnCallback);
//                    Toast.makeText(LoginPage.this, ex.getMessage()+ "登录失败", Toast.LENGTH_SHORT).show();
//                }
//
//                @Override
//                public void onFinished() {
//                    super.onFinished();
//                }
//            });
//        }
//
//        @Override
//        public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
//
//            Toast.makeText(LoginPage.this, "授权失败", Toast.LENGTH_SHORT).show();
//        }
//
//        @Override
//        public void onCancel(SHARE_MEDIA share_media, int i) {
//            Toast.makeText(LoginPage.this, "授权取消", Toast.LENGTH_SHORT).show();
//        }
//    };

}
